<?php
/**
 * Created by PhpStorm.
 * User: DQ
 * Date: 2018/7/12
 * Time: 12:54
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;
use App\Services\ThirdPayments\BankTraits\QFBankMap;

class QIFPayment extends RechargeBaseAbstract implements WAPable
{
    use QFBankMap;

    protected $gw_pay = 'http://zf.x120.top/apisubmit';
    protected $gw_query = 'http://zf.x120.top/apiorderquery';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['status'] == 1) return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr = "";
        $signStr .= "customerid=" . $data['customerid'] . '&';
        $signStr .= "status=" . $data['status'] . '&';
        $signStr .= "sdpayno=" . $data['sdpayno'] . '&';
        $signStr .= "sdorderno=" . $data['sdorderno'] . '&';
        $signStr .= "total_fee=" . $data['total_fee'] . '&';
        $signStr .= "paytype=" . $data['paytype'] . '&';
        $signStr .= $this->getMchKey();
        if (strcasecmp($data['sign'], md5($signStr)) == 0) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['sdorderno'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['total_fee'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['sdpayno'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::QIFENG;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::BANK => 'bank',
            PaymentMap::ALI_WAP => 'aliwap',
            PaymentMap::ALI => 'alipay',
            PaymentMap::WX => 'weixin',
            PaymentMap::WX_WAP => 'wxh5',
            PaymentMap::QQ => 'qqrcode',
            PaymentMap::QQ_WAP => 'qqwallet',
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('customerid', $this->getMchId());
        $this->setParameter('sdorderno', $rechargeOrder->plat_no);
        $this->setParameter('reqtime', date('YmdHis'));
        $this->setParameter('sign', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            $body = json_encode($res['body'], true);
            if ($body['status'] == 1) return true;
            return $body['msg'];
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() .'?' . http_build_query($this->getParameters());
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() .'?' . http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr = "";
        $signStr .= "version=" . $this->getParameter('version') . '&';
        $signStr .= "customerid=" . $this->getParameter('customerid') . '&';
        $signStr .= "total_fee=" . $this->getParameter('total_fee') . '&';
        $signStr .= "sdorderno=" . $this->getParameter('sdorderno') . '&';
        $signStr .= "notifyurl=" . $this->getParameter('notifyurl') . '&';
        $signStr .= "returnurl=" . $this->getParameter('returnurl') . '&';
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = "";
        $signStr .= "customerid=" . $this->getParameter('customerid') .'&';
        $signStr .= "sdorderno=" . $this->getParameter('sdorderno') .'&';
        $signStr .= "reqtime=" . $this->getParameter('reqtime') .'&';
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        $this->setParameter('version', '1.0');
        $this->setParameter('customerid', $this->getMchId());
        $this->setParameter('sdorderno', $rechargeOrder->plat_no);
        $this->setParameter('total_fee', $rechargeOrder->order_amt);
        $this->setParameter('paytype', $this->getPaymentMap($mchData['recharge_type']));
        if ($mchData['recharge_type'] == PaymentMap::BANK) {
            $this->setParameter('bankcode', $this->getBank($mchData['bank_code']));
        }
        $this->setParameter('notifyurl', $this->getCallbackUrl());
        $this->setParameter('returnurl', $this->getReturnUrl());
        $this->setParameter('sign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() .'?' . http_build_query($this->getParameters()));
    }
}